//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.activiti.engine.impl.persistence.entity.data.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ActivitiOptimisticLockingException;
import org.activiti.engine.impl.ExecutionQueryImpl;
import org.activiti.engine.impl.Page;
import org.activiti.engine.impl.ProcessInstanceQueryImpl;
import org.activiti.engine.impl.cfg.PerformanceSettings;
import org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.activiti.engine.impl.persistence.CachedEntityMatcher;
import org.activiti.engine.impl.persistence.SingleCachedEntityMatcher;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.activiti.engine.impl.persistence.entity.data.AbstractDataManager;
import org.activiti.engine.impl.persistence.entity.data.ExecutionDataManager;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.ExecutionByProcessInstanceMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.ExecutionsByParentExecutionIdAndActivityIdEntityMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.ExecutionsByParentExecutionIdEntityMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.ExecutionsByProcessInstanceIdEntityMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.ExecutionsByRootProcessInstanceMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.ExecutionsWithSameRootProcessInstanceIdMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.InactiveExecutionsByProcInstMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.InactiveExecutionsInActivityAndProcInstMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.InactiveExecutionsInActivityMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.ProcessInstancesByProcessDefinitionMatcher;
import org.activiti.engine.impl.persistence.entity.data.impl.cachematcher.SubProcessInstanceExecutionBySuperExecutionIdMatcher;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;

public class MybatisExecutionDataManager extends AbstractDataManager<ExecutionEntity> implements ExecutionDataManager {
  protected PerformanceSettings performanceSettings;
  protected CachedEntityMatcher<ExecutionEntity> executionsByParentIdMatcher = new ExecutionsByParentExecutionIdEntityMatcher();
  protected CachedEntityMatcher<ExecutionEntity> executionsByProcessInstanceIdMatcher = new ExecutionsByProcessInstanceIdEntityMatcher();
  protected SingleCachedEntityMatcher<ExecutionEntity> subProcessInstanceBySuperExecutionIdMatcher = new SubProcessInstanceExecutionBySuperExecutionIdMatcher();
  protected CachedEntityMatcher<ExecutionEntity> executionsWithSameRootProcessInstanceIdMatcher = new ExecutionsWithSameRootProcessInstanceIdMatcher();
  protected CachedEntityMatcher<ExecutionEntity> inactiveExecutionsInActivityAndProcInstMatcher = new InactiveExecutionsInActivityAndProcInstMatcher();
  protected CachedEntityMatcher<ExecutionEntity> inactiveExecutionsByProcInstMatcher = new InactiveExecutionsByProcInstMatcher();
  protected CachedEntityMatcher<ExecutionEntity> inactiveExecutionsInActivityMatcher = new InactiveExecutionsInActivityMatcher();
  protected CachedEntityMatcher<ExecutionEntity> executionByProcessInstanceMatcher = new ExecutionByProcessInstanceMatcher();
  protected CachedEntityMatcher<ExecutionEntity> executionsByRootProcessInstanceMatcher = new ExecutionsByRootProcessInstanceMatcher();
  protected CachedEntityMatcher<ExecutionEntity> executionsByParentExecutionIdAndActivityIdEntityMatcher = new ExecutionsByParentExecutionIdAndActivityIdEntityMatcher();
  protected CachedEntityMatcher<ExecutionEntity> processInstancesByProcessDefinitionMatcher = new ProcessInstancesByProcessDefinitionMatcher();

  public MybatisExecutionDataManager(ProcessEngineConfigurationImpl processEngineConfiguration) {
    super(processEngineConfiguration);
    this.performanceSettings = processEngineConfiguration.getPerformanceSettings();
  }

  public Class<? extends ExecutionEntity> getManagedEntityClass() {
    return ExecutionEntityImpl.class;
  }

  public ExecutionEntity create() {
    return ExecutionEntityImpl.createWithEmptyRelationshipCollections();
  }

  public ExecutionEntity findById(String entityId) {
    return this.performanceSettings.isEnableEagerExecutionTreeFetching() ? this.findByIdAndFetchExecutionTree(entityId) : (ExecutionEntity)super.findById(entityId);
  }

  protected ExecutionEntity findByIdAndFetchExecutionTree(String executionId) {
    ExecutionEntity cachedEntity = (ExecutionEntity)this.getEntityCache().findInCache(this.getManagedEntityClass(), executionId);
    if (cachedEntity != null) {
      return cachedEntity;
    } else {
      List<ExecutionEntity> executionEntities = this.getList("selectExecutionsWithSameRootProcessInstanceId", executionId, this.executionsWithSameRootProcessInstanceIdMatcher, true);
      Iterator var4 = executionEntities.iterator();

      ExecutionEntity executionEntity;
      do {
        if (!var4.hasNext()) {
          return null;
        }

        executionEntity = (ExecutionEntity)var4.next();
      } while(!executionId.equals(executionEntity.getId()));

      return executionEntity;
    }
  }

  public ExecutionEntity findSubProcessInstanceBySuperExecutionId(String superExecutionId) {
    if (this.performanceSettings.isEnableEagerExecutionTreeFetching()) {
      this.findByIdAndFetchExecutionTree(superExecutionId);
    }

    return (ExecutionEntity)this.getEntity("selectSubProcessInstanceBySuperExecutionId", superExecutionId, this.subProcessInstanceBySuperExecutionIdMatcher, !this.performanceSettings.isEnableEagerExecutionTreeFetching());
  }

  public List<ExecutionEntity> findChildExecutionsByParentExecutionId(String parentExecutionId) {
    if (this.performanceSettings.isEnableEagerExecutionTreeFetching()) {
      this.findByIdAndFetchExecutionTree(parentExecutionId);
      return this.getListFromCache(this.executionsByParentIdMatcher, parentExecutionId);
    } else {
      return this.getList("selectExecutionsByParentExecutionId", parentExecutionId, this.executionsByParentIdMatcher, true);
    }
  }

  public List<ExecutionEntity> findChildExecutionsByProcessInstanceId(String processInstanceId) {
    if (this.performanceSettings.isEnableEagerExecutionTreeFetching()) {
      this.findByIdAndFetchExecutionTree(processInstanceId);
      return this.getListFromCache(this.executionsByProcessInstanceIdMatcher, processInstanceId);
    } else {
      return this.getList("selectChildExecutionsByProcessInstanceId", processInstanceId, this.executionsByProcessInstanceIdMatcher, true);
    }
  }

  public List<ExecutionEntity> findExecutionsByParentExecutionAndActivityIds(String parentExecutionId, Collection<String> activityIds) {
    Map<String, Object> parameters = new HashMap(2);
    parameters.put("parentExecutionId", parentExecutionId);
    parameters.put("activityIds", activityIds);
    if (this.performanceSettings.isEnableEagerExecutionTreeFetching()) {
      this.findByIdAndFetchExecutionTree(parentExecutionId);
      return this.getListFromCache(this.executionsByParentExecutionIdAndActivityIdEntityMatcher, parameters);
    } else {
      return this.getList("selectExecutionsByParentExecutionAndActivityIds", parameters, this.executionsByParentExecutionIdAndActivityIdEntityMatcher, true);
    }
  }

  public List<ExecutionEntity> findExecutionsByRootProcessInstanceId(String rootProcessInstanceId) {
    if (this.performanceSettings.isEnableEagerExecutionTreeFetching()) {
      this.findByIdAndFetchExecutionTree(rootProcessInstanceId);
      return this.getListFromCache(this.executionsByRootProcessInstanceMatcher, rootProcessInstanceId);
    } else {
      return this.getList("selectExecutionsByRootProcessInstanceId", rootProcessInstanceId, this.executionsByRootProcessInstanceMatcher, true);
    }
  }

  public List<ExecutionEntity> findExecutionsByProcessInstanceId(String processInstanceId) {
    if (this.performanceSettings.isEnableEagerExecutionTreeFetching()) {
      this.findByIdAndFetchExecutionTree(processInstanceId);
      return this.getListFromCache(this.executionByProcessInstanceMatcher, processInstanceId);
    } else {
      return this.getList("selectExecutionsByProcessInstanceId", processInstanceId, this.executionByProcessInstanceMatcher, true);
    }
  }

  public Collection<ExecutionEntity> findInactiveExecutionsByProcessInstanceId(String processInstanceId) {
    HashMap<String, Object> params = new HashMap(2);
    params.put("processInstanceId", processInstanceId);
    params.put("isActive", false);
    if (this.performanceSettings.isEnableEagerExecutionTreeFetching()) {
      this.findByIdAndFetchExecutionTree(processInstanceId);
      return this.getListFromCache(this.inactiveExecutionsByProcInstMatcher, params);
    } else {
      return this.getList("selectInactiveExecutionsForProcessInstance", params, this.inactiveExecutionsByProcInstMatcher, true);
    }
  }

  public Collection<ExecutionEntity> findInactiveExecutionsByActivityIdAndProcessInstanceId(String activityId, String processInstanceId) {
    HashMap<String, Object> params = new HashMap(3);
    params.put("activityId", activityId);
    params.put("processInstanceId", processInstanceId);
    params.put("isActive", false);
    if (this.performanceSettings.isEnableEagerExecutionTreeFetching()) {
      this.findByIdAndFetchExecutionTree(processInstanceId);
      return this.getListFromCache(this.inactiveExecutionsInActivityAndProcInstMatcher, params);
    } else {
      return this.getList("selectInactiveExecutionsInActivityAndProcessInstance", params, this.inactiveExecutionsInActivityAndProcInstMatcher, true);
    }
  }

  public List<String> findProcessInstanceIdsByProcessDefinitionId(String processDefinitionId) {
    return this.getDbSqlSession().selectList("selectProcessInstanceIdsByProcessDefinitionId", processDefinitionId, false);
  }

  public long findExecutionCountByQueryCriteria(ExecutionQueryImpl executionQuery) {
    return (Long)this.getDbSqlSession().selectOne("selectExecutionCountByQueryCriteria", executionQuery);
  }

  public List<ExecutionEntity> findExecutionsByQueryCriteria(ExecutionQueryImpl executionQuery, Page page) {
    return this.getDbSqlSession().selectList("selectExecutionsByQueryCriteria", executionQuery, page, !this.performanceSettings.isEnableEagerExecutionTreeFetching());
  }

  public long findProcessInstanceCountByQueryCriteria(ProcessInstanceQueryImpl executionQuery) {
    return (Long)this.getDbSqlSession().selectOne("selectProcessInstanceCountByQueryCriteria", executionQuery);
  }

  public List<ProcessInstance> findProcessInstanceByQueryCriteria(ProcessInstanceQueryImpl executionQuery) {
    return this.getDbSqlSession().selectList("selectProcessInstanceByQueryCriteria", executionQuery, !this.performanceSettings.isEnableEagerExecutionTreeFetching());
  }

  public List<ProcessInstance> findProcessInstanceAndVariablesByQueryCriteria(ProcessInstanceQueryImpl executionQuery) {
    if (executionQuery.getFirstResult() >= 0 && executionQuery.getMaxResults() > 0) {
      int firstResult = executionQuery.getFirstResult();
      int maxResults = executionQuery.getMaxResults();
      if (executionQuery.getProcessInstanceVariablesLimit() != null) {
        executionQuery.setMaxResults(executionQuery.getProcessInstanceVariablesLimit());
      } else {
        executionQuery.setMaxResults(this.getProcessEngineConfiguration().getExecutionQueryLimit());
      }

      executionQuery.setFirstResult(0);
      List<ProcessInstance> instanceList = this.getDbSqlSession().selectListWithRawParameterWithoutFilter("selectProcessInstanceWithVariablesByQueryCriteria", executionQuery, executionQuery.getFirstResult(), executionQuery.getMaxResults());
      if (instanceList != null && !instanceList.isEmpty()) {
        int toIndex;
        if (firstResult > 0) {
          if (firstResult <= instanceList.size()) {
            toIndex = firstResult + Math.min(maxResults, instanceList.size() - firstResult);
            return instanceList.subList(firstResult, toIndex);
          } else {
            return Collections.EMPTY_LIST;
          }
        } else {
          toIndex = Math.min(maxResults, instanceList.size());
          return instanceList.subList(0, toIndex);
        }
      } else {
        return Collections.EMPTY_LIST;
      }
    } else {
      return Collections.EMPTY_LIST;
    }
  }

  public List<Execution> findExecutionsByNativeQuery(Map<String, Object> parameterMap, int firstResult, int maxResults) {
    return this.getDbSqlSession().selectListWithRawParameter("selectExecutionByNativeQuery", parameterMap, firstResult, maxResults);
  }

  public List<ProcessInstance> findProcessInstanceByNativeQuery(Map<String, Object> parameterMap, int firstResult, int maxResults) {
    return this.getDbSqlSession().selectListWithRawParameter("selectExecutionByNativeQuery", parameterMap, firstResult, maxResults);
  }

  public long findExecutionCountByNativeQuery(Map<String, Object> parameterMap) {
    return (Long)this.getDbSqlSession().selectOne("selectExecutionCountByNativeQuery", parameterMap);
  }

  public void updateExecutionTenantIdForDeployment(String deploymentId, String newTenantId) {
    HashMap<String, Object> params = new HashMap();
    params.put("deploymentId", deploymentId);
    params.put("tenantId", newTenantId);
    this.getDbSqlSession().update("updateExecutionTenantIdForDeployment", params);
  }

  public void updateProcessInstanceLockTime(String processInstanceId, Date lockDate, Date expirationTime) {
    HashMap<String, Object> params = new HashMap();
    params.put("id", processInstanceId);
    params.put("lockTime", lockDate);
    params.put("expirationTime", expirationTime);
    int result = this.getDbSqlSession().update("updateProcessInstanceLockTime", params);
    if (result == 0) {
      throw new ActivitiOptimisticLockingException("Could not lock process instance");
    }
  }

  public void updateAllExecutionRelatedEntityCountFlags(boolean newValue) {
    this.getDbSqlSession().update("updateExecutionRelatedEntityCountEnabled", newValue);
  }

  public void clearProcessInstanceLockTime(String processInstanceId) {
    HashMap<String, Object> params = new HashMap();
    params.put("id", processInstanceId);
    this.getDbSqlSession().update("clearProcessInstanceLockTime", params);
  }
}
